

import Foundation

extension Dictionary where Key == String {
    func stringValue(_ key: String) -> String {
        guard let value = self[key] else { return "" }
        if let str = value as? String {
            return str
        } else if let intVal = value as? Int {
            return String(intVal)
        } else if let doubleVal = value as? Double {
            return String(doubleVal)
        } else if let floatVal = value as? Float {
            return String(floatVal)
        } else if let boolVal = value as? Bool {
            return String(boolVal)
        }
        return String(describing: value)
    }
    
    func intValue(_ key: Key) -> Int {
        guard let value = self[key] else { return 0 }
        if let intVal = value as? Int {
            return intVal
        } else if let str = value as? String {
            return Int(str) ?? 0
        } else if let doubleVal = value as? Double {
            return Int(doubleVal)
        } else if let floatVal = value as? Float {
            return Int(floatVal)
        } else if let boolVal = value as? Bool {
            return boolVal ? 1 : 0
        }
        return 0
    }
    
    
    func int64Value(_ key: Key) -> Int64 {
        guard let value = self[key] else { return 0 }
        if let intVal = value as? Int64 {
            return intVal
        } else if let str = value as? String {
            return Int64(str) ?? 0
        } else if let doubleVal = value as? Double {
            return Int64(doubleVal)
        } else if let floatVal = value as? Float {
            return Int64(floatVal)
        } else if let boolVal = value as? Bool {
            return boolVal ? 1 : 0
        }
        return 0
    }
    
    func doubleValue(_ key: Key) -> Double {
        guard let value = self[key] else { return 0 }
        if let doubleVal = value as? Double {
            return doubleVal
        } else if let intVal = value as? Int {
            return Double(intVal)
        } else if let str = value as? String {
            return Double(str) ?? 0
        }
        return 0
    }
    
    func boolValue(_ key: Key) -> Bool {
        guard let value = self[key] else { return false }
        if let boolVal = value as? Bool {
            return boolVal
        } else if let str = value as? String {
            return (str as NSString).boolValue
        } else if let intVal = value as? Int {
            return intVal != 0
        }
        return false
    }
    
    func arrayValue<T>(_ key: String, of type: T.Type) -> [T] {
         return self[key] as? [T] ?? []
     }
     
     func dictionaryValue(_ key: String) -> [String: Any] {
         return self[key] as? [String: Any] ?? [:]
     }
}

extension Dictionary where Key == String {
    /// 调试用：检查路径是否存在
    func debugPath(_ path: String) {
        let keys = path.split(separator: ".").map { String($0) }
        var current: Any? = self
        for key in keys {
            if let dict = current as? [String: Any],
               let next = dict[key] {
                print("✅ 找到 key: \(key) = \(next)")
                current = next
            } else {
                print("❌ 路径解析失败: \(path)，卡在 \(key)")
                return
            }
        }
        print("🎯 路径 \(path) 检查通过")
    }
}

extension Dictionary where Key == String, Value == Any {
    
    func toJSONData() -> Data? {
        guard JSONSerialization.isValidJSONObject(self) else { return nil }
        return try? JSONSerialization.data(withJSONObject: self, options: [])
    }
    
    func toJSONString() -> String? {
        guard let data = toJSONData() else { return nil }
        return String(data: data, encoding: .utf8)
    }
}
